package cn.org.timespace.easyframe.util.tag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.foundation.expression.Operator;
import cn.org.timespace.easyframe.util.EfResourceContext;
import cn.org.timespace.easyframe.util.dictionary.EfDictionary;
import cn.org.timespace.easyframe.util.dictionary.EfDictionaryDAO;

public class Translate {
	
	private static final Log log = LogFactory.getLog(Translate.class);
	
	private static final String LOADING_DICTIONARY_TYPE = "loading.dictionary.type";
	
	private static enum LoadingDictionaryType{
		onlyone,multi	
	}
	
	private static Translate translate ;
	
	// 缓存字典表的数据
	private static Map<String, List<EfDictionary>> dicMap = new HashMap<String, List<EfDictionary>>();
	
	private Translate(){
		
		this.clean();
		
		dicMap=EfDictionaryDAO.getInstance().getAll();
		
		if(dicMap != null){
			log.info("共加载字典分组："+dicMap.size());
			
			Iterator<String> ite = dicMap.keySet().iterator();
			int total = 0;
			while(ite.hasNext()){
				String key = ite.next();
				int i = dicMap.get(key).size();
				total += i;
				log.info("GroupName："+key +"  项："+i);
			}
			log.info("共加载项："+total);
		}
		
	}
	
	public static Translate getInstance() {
		String cfgLoadingType = EfResourceContext.getInstance().getParamValue(LOADING_DICTIONARY_TYPE);
		
		if(LoadingDictionaryType.multi.toString().equals(cfgLoadingType)){
			translate = new Translate();
		} else {
			if(translate == null){
				translate = new Translate();
			}
		}
		return translate;
	}
	
	/**
	 * 根据分组名称(groupName)从缓存字典中获取数据
	 * @param groupName
	 * @return
	 */
	public List<EfDictionary> getDataListByTable(String groupName){
		List<EfDictionary> dicList = new ArrayList<EfDictionary>();
		if(groupName != null && groupName.length() > 0){
			dicList = dicMap.get(groupName);
			if(dicList == null || dicList.size() == 0){
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
			}
		}
		return dicList;
	}
	
	/**
	 * 根据分组名称(groupName)，过滤条件(filter)从缓存字典中获取数据
	 * @param groupName
	 * @return
	 */
	public List<EfDictionary> getDataListByTable(String groupName,String filter){
		List<EfDictionary> dicList = new ArrayList<EfDictionary>();
		if(groupName != null && groupName.length() > 0){
			dicList = dicMap.get(groupName);
			if(dicList == null || dicList.size() == 0){
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
			}
		}
		
		if(filter != null && !filter.equals("")){
			String oopr = "";
			for( String opr : Operator.getAll())
			{
				if(filter.indexOf(opr) > -1) {
					oopr = opr;
					break;
				}
			}
			
			String opr = filter.substring(0,oopr.length());
			int value = Integer.parseInt(filter.substring(oopr.length(),filter.length()));
			
			if(dicList != null && dicList.size() > 0)
			{
				List<EfDictionary> filterDicList = new ArrayList<EfDictionary>();
				for(EfDictionary efDic : dicList)
				{
					Integer iFilter = null;
					if(efDic.getFilter() != null && !efDic.getFilter().equals("")) 
					{
						iFilter = Integer.parseInt(efDic.getFilter());
						if(opr.equals(Operator.GREATER_EQUAL)){
							if(iFilter >= value) filterDicList.add(efDic);
						}else if(opr.equals(Operator.LESS_EQUAL)){
							if(iFilter <= value) filterDicList.add(efDic);
						}else if(opr.equals(Operator.UNEQUAL)){
							if(iFilter != value) filterDicList.add(efDic);
						}else if(opr.equals(Operator.GREATER)){
							if(iFilter > value) filterDicList.add(efDic);
						}else if(opr.equals(Operator.LESS)){
							if(iFilter < value) filterDicList.add(efDic);
						}else if(opr.equals(Operator.EQUAL)){
							if(iFilter == value) filterDicList.add(efDic);
						}
					}
				}
				
				if(filterDicList != null && filterDicList.size() > 0)
				{
					dicList = filterDicList;
				}
			}
		}
		
		return dicList;
	}
	
	/**
	 * 根据分组名称(groupName)从缓存字典中获取数据
	 * @param groupName
	 * @return
	 */
	public List<EfDictionary> getDataListBySql(String groupName){
		List<EfDictionary> dicList = new ArrayList<EfDictionary>();
		if(groupName != null && groupName.length() > 0){
			List<EfDictionary> oneList = dicMap.get(groupName);
			if(oneList == null || oneList.size() == 0){
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
			}else{
				String sql = oneList.get(0).getExesql();
				String remark = oneList.get(0).getRemark();
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">获取的动态SQL是："+sql);
				dicList = EfDictionaryDAO.getInstance().getDictionaryBySql(sql,remark);
			}
		}
		return dicList;
	}
	
	/**
	 * 根据分组名称(groupName),过滤条件(where)从缓存字典中获取数据
	 * @param groupName
	 * @return
	 */
	public List<EfDictionary> getDataListBySql(String groupName,String where){
		List<EfDictionary> dicList = new ArrayList<EfDictionary>();
		if(groupName != null && groupName.length() > 0){
			List<EfDictionary> oneList = dicMap.get(groupName);
			if(oneList == null || oneList.size() == 0){
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
			}else{
				String sql = oneList.get(0).getExesql();
				String remark = oneList.get(0).getRemark();
				if (log.isInfoEnabled()) log.info("根据<"+groupName+">获取的动态SQL是："+sql);
				
				if(where != null && !where.equals(""))
				{
					sql = sql +" " + where;
				}
				
				dicList = EfDictionaryDAO.getInstance().getDictionaryBySql(sql,remark);
			}
		}
		return dicList;
	}	
	
	/**
	 * 根据分组名、值获取名称
	 * @param groupName
	 * @param value
	 * @return
	 */
	public String getNameByTable(String groupName,String value){
		String res = "";
		
		List<EfDictionary> dicList = dicMap.get(groupName);
		if(dicList != null){
			for(EfDictionary dic : dicList){
				String itemValue = dic.getItemvalue();
				if(itemValue.equals(value)) {
					res = dic.getItemname();
					continue;
				}
			}
		}else{
			if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
		}
		
		return res;
	}
	
	/**
	 * 根据分组名、值获取名称
	 * @param groupName
	 * @param value
	 * @return
	 */
	public String getNameBySql(String groupName,String value){
		String res = "";
		
		List<EfDictionary> dicList = dicMap.get(groupName);
		if(dicList != null){
			String sql = dicList.get(0).getExesql();
			sql = "select * from ("+sql+") t where t.itemvalue = "+value;
			
			if (log.isInfoEnabled()) log.info("根据<"+groupName+">翻译字典项，翻译sql是："+sql);
			
			res = EfDictionaryDAO.getInstance().getNameBySql(sql);
			
		}else{
			if (log.isInfoEnabled()) log.info("根据<"+groupName+">没有找到合适的字典项，请检查groupName是否正确。");
		}
		
		return res;
	}
	
	public void clean(){
		if(log.isInfoEnabled()) log.info("正在清除缓存中的字典表数据...");
		dicMap.clear();
		if(dicMap.size() == 0){
			if(log.isInfoEnabled()) log.info("清除缓存中的字典表数据成功。");
		}else{
			if(log.isInfoEnabled()) log.info("清除缓存中的字典表数据失败。");
		}
	}

}